﻿using System.Web.Mvc;
using YunSpace.Application.SystemManage;
using YunSpace.Application.WorkFlow;
using YunSpace.Core;
using YunSpace.Domain.Entity.WorkFlow;

namespace YunSpace.Web.Areas.WorkFlowSetting.Controllers
{
    /// <summary>
    /// 流程管理
    /// </summary>
    public class FlowManageController : ControllerBase
    {
        private readonly FlowManageApp _flowManageApp = new FlowManageApp();
        private readonly ItemsDetailApp _itemsDetailApp = new ItemsDetailApp();
        private readonly UserApp _userApp = new UserApp();
        private readonly RoleApp _roleApp = new RoleApp();

        #region WorkFlow

        /// <summary>
        /// 流程管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult WorkFlow()
        {
            return View();
        }

        /// <summary>
        /// 获取流程管理列表数据
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetWorkFlowGridJson(Pagination pagination)
        {
            var data = new
            {
                rows = _flowManageApp.GetFlowManages(pagination),
                pagination.total,
                pagination.page,
                pagination.records
            };

            return Content(data.ToJson());
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult EditWorkFlow()
        {
            return View();
        }

        /// <summary>
        /// 获取流程管理数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult GetWorkFlowJson(string keyValue)
        {
            var data = _flowManageApp.GetWorkFlow(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="workFlow"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult SaveWorkFlow(WorkFlowEntity workFlow, string keyValue)
        {
            bool result;

            if (keyValue.IsEmpty())
            {
                result = _flowManageApp.AddWorkFlow(workFlow);
                return result ? Success("操作成功。") : Error("已经存在同样的“流程表单”");
            }

            result = _flowManageApp.UpdateWorkFlow(workFlow, keyValue);
            return result ? Success("操作成功。") : Error("已经存在同样的“流程表单”");
        }

        public ActionResult DeteleWorkFlow(string keyValue)
        {
            _flowManageApp.DeleteWorkFlow(keyValue);
            return Success("操作成功。");
        }

        #endregion WorkFlow

        #region WorkFlowDetail

        /// <summary>
        /// 流程管理详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult WorkFlowDetail()
        {
            return View();
        }

        /// <summary>
        /// 获取流程管理详情列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetWorkFlowDetailGridJson(Pagination pagination, string keyValue)
        {
            var data = new
            {
                rows = _flowManageApp.GetFlowManageSteps(pagination, keyValue),
                pagination.total,
                pagination.page,
                pagination.records,
                //workFlow = _flowManageApp.GetWorkFlow(keyValue)
            };

            return Content(data.ToJson());
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditWorkFlowDetail()
        {
            return View();
        }

        /// <summary>
        /// 获取流程管理详情数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetWorkFlowDetailJson(string keyValue)
        {
            var data = _flowManageApp.GetWorkFlowDetail(keyValue);
            return Content(data.ToJson());
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult SaveWorkFlowDetail(WorkFlowDetailEntity entity, string keyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                _flowManageApp.AddWorkFlowDetail(entity);
                return Success("操作成功。");
            }

            _flowManageApp.UpdateFlowManageInfo(entity, keyValue);
            return Success("操作成功。");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult DeleteWorkFlowDetail(string keyValue)
        {
            _flowManageApp.DeleteWorkFlowDetail(keyValue);
            return Success("操作成功。");
        }

        #endregion WorkFlowDetail

        /// <summary>
        /// 获取字典项详情项
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDictionaryItemsJson()
        {
            var data = new
            {
                F_TimeOutTip = _itemsDetailApp.GetItemList("cqts"),
                F_IsFishTime = _itemsDetailApp.GetItemList(""),
                F_TimeOutOpreation = _itemsDetailApp.GetItemList("cscl"),
                F_OpreationType = _itemsDetailApp.GetItemList("clfs"),
                F_OpreationStrategy = _itemsDetailApp.GetItemList("clcl"),
                F_ReturnStrategy = _itemsDetailApp.GetItemList("thcl"),
                F_ReturnType = _itemsDetailApp.GetItemList("thlx"),
                F_MessageSend = _itemsDetailApp.GetItemList("xxfsfs"),
                F_WorkFormId = _itemsDetailApp.GetItemList("lcbd"),
                F_Users = _userApp.GetUsers(null),
                F_Roles = _roleApp.GetList(),
                F_PersonType = _itemsDetailApp.GetItemList("OperationType"),
                F_OperationPersonType = _itemsDetailApp.GetItemList("OperationPersonType")
            };

            return Content(data.ToJson());
        }

        ///// <summary>
        ///// 根据 id 获取详情
        ///// </summary>
        ///// <param name="keyValue"></param>
        ///// <returns></returns>
        //public ActionResult GetDetailByIdJson(string keyValue)
        //{
        //    var data = _flowManageApp.GetWorkFlowDetail(keyValue);
        //    return Content(data.ToJson());
        //}

        //public ActionResult GetWorkFlowByIdJson(string keyValue)
        //{
        //    var data = _flowManageApp.GetWorkFlow(keyValue);
        //    return Content(data.ToJson());
        //}

        //public ActionResult GetEditWorkFlowJson(string keyValue)
        //{
        //    var data = _flowManageApp.GetWorkFlow(keyValue);
        //    return Content(data.ToJson());
        //}
    }
}